package com.ling.service;

import com.ling.bean.Question;
import com.ling.bean.StarQuestion;
import com.ling.bean.StarQuestionExample;
import com.ling.dao.StarQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StarQuestionService {
    private final StarQuestionMapper starQuestionMapper;

    @Autowired
    public StarQuestionService(StarQuestionMapper starQuestionMapper) {
        this.starQuestionMapper = starQuestionMapper;
    }

    //获取指定用户关注的问题
    public List<Question> getStarQuestion(String userId) {
        return starQuestionMapper.selectByUserIdWithQuestion(userId);
    }

    //删除指定用户关注的某个问题
    public void delStarQuestionById(String userId, String questionId) {
        StarQuestionExample starQuestionExample = new StarQuestionExample();
        starQuestionExample.createCriteria().andUseridEqualTo(userId).andQuestionidEqualTo(questionId);
        starQuestionMapper.deleteByExample(starQuestionExample);
    }

    //增加用户关注某个问题
    public void addStarQuestion(StarQuestion starQuestion) {
        starQuestionMapper.insertSelective(starQuestion);
    }

    //判断是否已经关注
    public boolean hasStarred(String userId,String questionId) {
        StarQuestionExample starQuestionExample = new StarQuestionExample();
        starQuestionExample.createCriteria().andUseridEqualTo(userId).andQuestionidEqualTo(questionId);
        return !starQuestionMapper.selectByExample(starQuestionExample).isEmpty();
    }
}
